/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import db.NoteDb;
import db.NoteInterface;
import java.util.ArrayList;
import models.Note;

/**
 *
 * @author Pelle
 * Edited Steffen
 * 
 * 21-06-2011, Pelle: Added getNoteCount & searchNotes
 */
public class NoteController {
    
    // Noten som vi arbejder med lige nu
    private Note note = null;
    private NoteInterface noteDb = new NoteDb();
    
    // Ny note
    public NoteController() {
        note = new Note();
    }
    
    // Åbner en note fra et id
    public NoteController(int id) {
        if(id > 0) {
            note = this.noteDb.getNote(id);
        }
    }
    
    // Åbner en ny note fra et note-objekt
    public NoteController(Note note) {
        if(note != null && note.getId() > 0) {
            int id = note.getId();
            note = this.noteDb.getNote(id);
        }
    }
    
    // Søg i noter
    public ArrayList<Note> searchNotes(String text, int isPublic) {
        ArrayList<Note> notes = new ArrayList<Note>();
        if(text.length() > 0) {
            notes = this.noteDb.searchNotes(text, isPublic);
        }
        
        return notes;
    }
    
    // Gem en note
    public int saveNote(String headline, String text, boolean isPublic) {
        this.note.setTitle(headline);
        this.note.setText(text);
        this.note.setPublic(isPublic);
        
        return this.noteDb.saveNote(this.note);
    }
    
    // Returnere noten vi arbejder med
    public Note getNote() {
        return this.note;
    }
    
    // Få antallet af noter tilgængelig
    public int getNoteCount() {
        return this.noteDb.getNoteCount();
    }
}
